package top.itmeans.yes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.itmeans.yes.entity.HomeAdvertise;
import top.itmeans.yes.mapper.HomeAdvertiseMapper;
import top.itmeans.yes.service.HomeAdvertiseService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 首页轮播广告表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class HomeAdvertiseServiceImpl extends ServiceImpl<HomeAdvertiseMapper, HomeAdvertise> implements HomeAdvertiseService {

    @Resource
    private HomeAdvertiseMapper advertiseMapper;

    @Override
    public int create(HomeAdvertise advertise) {
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        return advertiseMapper.insert(advertise);
    }

    @Override
    public int delete(List<Long> ids) {
        return advertiseMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        HomeAdvertise record = new HomeAdvertise();
        record.setId(id);
        record.setStatus(status);
        return advertiseMapper.updateById(record);
    }

    @Override
    public HomeAdvertise getItem(Long id) {
        return advertiseMapper.selectById(id);
    }

    @Override
    public int update(Long id, HomeAdvertise advertise) {
        advertise.setId(id);
        return advertiseMapper.updateById(advertise);
    }

    @Override
    public Page<HomeAdvertise> list(String name, Integer type, String endTime, Integer pageSize, Integer pageNum) {
        Page<HomeAdvertise> homeAdvertisePage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like(HomeAdvertise::getName,name);
        }
        if (type != null) {
            queryWrapper.eq(HomeAdvertise::getType,type);
        }
        if (!StringUtils.isEmpty(endTime)) {
            String startStr = endTime + " 00:00:00";
            String endStr = endTime + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date start = null;
            try {
                start = sdf.parse(startStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date end = null;
            try {
                end = sdf.parse(endStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (start != null && end != null) {
                queryWrapper.between(HomeAdvertise::getEndTime,start,end);
            }
        }
        queryWrapper.orderByDesc(HomeAdvertise::getOrderCount);
        return advertiseMapper.selectPage(homeAdvertisePage,queryWrapper);
    }
}
